//
//  ForgotViewController.m
//  安好
//
//  Created by 冯凯 on 14-11-27.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "ForgotViewController.h"
#import "NewPasswordViewController.h"
@interface ForgotViewController ()
{
    UIButton *rightButton;
}
@end

@implementation ForgotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.isLogout) {
        self.navigationController.navigationBarHidden=YES;
    }else
    {
        self.navigationItem.title = @"忘记密码";
        UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
        backBtnImg.tintColor = [UIColor whiteColor];
        self.navigationItem.leftBarButtonItem = backBtnImg;
        
        rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = CGRectMake(-20,0, 55, 23);
        [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -30)];
        rightButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [rightButton setTitle:@"发送" forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    }
    

    
    self.forgetTextField.frame = CGRectMake(67, 0, kScreenWidth - 70, 46);
    self.backView.frame = CGRectMake(0, 64, kScreenWidth, 46);
    self.upLine.frame = CGRectMake(0, 0, kScreenWidth, 1);
    self.downLine.frame = CGRectMake(0, 45, kScreenWidth,1);
    
}
- (void)back:(UIButton *)sender
{
    if (self.isLogout) {
        self.navigationController.navigationBarHidden=NO;
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 100) {
        NewPasswordViewController * newpassword = [[NewPasswordViewController alloc]initWithNibName:@"NewPasswordViewController" bundle:nil];
        newpassword.userString = self.forgetTextField.text;
        [self.navigationController pushViewController:newpassword animated:YES];
    }
}
- (void)viewDidAppear:(BOOL)animated
{
    [self.forgetTextField becomeFirstResponder];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)next:(UIButton *)sender
{
    if (self.isLoad == NO) {
        self.isLoad = YES;
        NSString * postURL = [NSString stringWithFormat:@"%@user/resetpw/",kBaseURL];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters=@{@"platform": kPlatform,
                                   @"version":kVersion,
                                   @"email":self.forgetTextField.text
                                   };
        [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *status=[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"status"]];
            self.isLoad = NO;

            if ([status isEqualToString:@"1"]){
//                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"重置密码的邮件已发送至您的邮箱，请注意查收！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                alert.tag = 100;
//                [alert show];
                [self showCustomHudSingleLine:@"重置密码的邮件已发送至您的邮箱，请注意查收！"];
                [self performSelector:@selector(popView) withObject:nil afterDelay:1.5f];
            }else
            {
//                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"您所填写的邮箱未被注册，请重试或注册新用户！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//                alert.tag = 101;
//                [alert show];
                [self showCustomHudSingleLine:@"您所填写的邮箱未被注册，请重试或注册新用户！"];

                
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        }];
    }

}
- (void)popView
{
    NewPasswordViewController * newpassword = [[NewPasswordViewController alloc]initWithNibName:@"NewPasswordViewController" bundle:nil];
    newpassword.userString = self.forgetTextField.text;
    [self.navigationController pushViewController:newpassword animated:YES];
}
@end
